package exam.axhu.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import exam.axhu.entity.Course;
import exam.axhu.entity.CourseInfo;
import exam.axhu.entity.Student;
import exam.axhu.service.CourseService;
import exam.axhu.service.StudentService;


@SuppressWarnings("serial")
@Controller @Scope("prototype")
public class EditStudent extends ActionSupport{
//defaultClass: studentLogin
	@Resource StudentService studentService;
	@Resource CourseService courseService;
	
	private String studentID = (String)ActionContext.getContext().getSession().get("studentID");
	private Student student;
	private String searchInfo;
	
	public String getSearchInfo() {
		return searchInfo;
	}
	public void setSearchInfo(String searchInfo) {
		this.searchInfo = searchInfo;
	}
	public Student getStudent() {
		return student;
	}
	public void setStudent(Student student) {
		this.student = student;
	}
		
	
	public String editStudentA(){
		ActionContext.getContext().put("editstudent", studentService.find(student.getStudentID()));
		return "Edit";
	}
	

	public String editStudentB(){
		studentService.update(student);
		addActionMessage("修改成功!欢迎!");
		ActionContext.getContext().put("students", studentService.list());
		return "EditSucceed";
	}
	
	public String editStudents(){
		ActionContext.getContext().put("students", studentService.list());
		return "StudentInfo";
	}

	
	public String AddStudentA(){
		return "AddStudent";
	}
	
	public String AddStudentB(){
	    
		//if(student.getStudentID()!="" && student.getPassword()!="" && student.getName()!="")
		if(student.getStudentID().length()!=0 && student.getPassword().length()!=0 && student.getName().length()!=0)
		{
			
				if(studentService.find(student.getStudentID())==null)
				{	
					student.setJoinDate(new java.sql.Date(new java.util.Date().getTime()));
					studentService.save(student);
					addActionMessage("添加成功!欢迎!");
					ActionContext.getContext().put("students", studentService.list());
					return "AddSucceed";
				}
				else{ 
					addActionMessage("系统检测到该学生学号之前已经注册过!请检查");
					return "AddFailed";
				}
		}

		addActionError("用户名、密码、姓名不能为空!");
		return "AddFailed";
	}

	public String DeleteStudentA(){
		ActionContext.getContext().put("deleteStudent", studentService.find(student.getStudentID()));
		return "DeleteStudent";
	}
	
	public String DeleteStudentB(){
		if(student!=null)
			studentService.delete(student.getStudentID());
		ActionContext.getContext().put("students", studentService.list());
		return "DeleteSucceed";
	}
	
	public String Search(){
		//search by searchInfo, that is by studentID and name;
		if(searchInfo.length()!=0){
			if(studentService.find(searchInfo)!=null){
				//search by studentID
				ActionContext.getContext().put("findStudent", studentService.find(searchInfo));
				return "SearchSucceed";
			}
			if(studentService.findByName(searchInfo).size()!=0){
				ActionContext.getContext().put("findStudent", studentService.findByName(searchInfo));
				return "SearchSucceed";
			}
			ActionContext.getContext().put("students", studentService.list());
			addActionMessage("没有找到该学生记录！请输入学生姓名或学号~");
			return "SearchFailed";
		}
		
		ActionContext.getContext().put("students", studentService.list());
		addActionMessage("请输入学生姓名或学号");
		return 	"SearchFailed";
	}
	
	public String home(){
		List<Course> ems = courseService.listCoursesByStudentID(studentID);
		List<CourseInfo> courses = new ArrayList<CourseInfo>();
		if(ems!=null)
			for(Course em : ems)
				courses.add(courseService.findByCourseID(em.getCourseID()));
		ActionContext.getContext().put("courses",courses);
		return "home";
	}
	
	public String editStudentSelfA(){
		ActionContext.getContext().put("editstudent", studentService.find(studentID));
		return "Edit";
	}
	

	public String editStudentSelfB(){
		studentService.update(student);
		addActionMessage("修改成功!欢迎!");
		List<Course> ems = courseService.listCoursesByStudentID(studentID);
		List<CourseInfo> courses = new ArrayList<CourseInfo>();
		if(ems!=null)
			for(Course em : ems)
				courses.add(courseService.findByCourseID(em.getCourseID()));
		ActionContext.getContext().put("courses",courses);
		return "EditSucceed";
	}
}
